package com.gwt.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Widget;
import com.gwt.client.event.RequestJsonEvent;
import com.gwt.client.event.RequestJsonEvent.Handler;
import com.gwt.client.event.SlideTransitionEvent;
import com.gwt.client.util.json.FetchData;

public class Slide10 extends Composite implements Slide{

	private static final Binder binder = GWT.create(Binder.class);

	interface Binder extends UiBinder<Widget, Slide10> {/***/}

	private Slide.Presenter listener;
	private EventBus eventBus;
	private static final String[] feeds = {"googlecode","googlewebtoolkit","googledataapis","googlemapsapi","google-code-featured","googleajaxsearchapi"};
	private static final String URL = ".blogspot.com/feeds/posts/default?alt=json-in-script&callback=";

	@UiField FocusPanel slide;
	@UiField ListBox feedPicker;
	@UiField Button jsonpRequestButton;
	@UiField DialogBox infoBox;
	@UiField HTML infoBoxLabel;
	@UiField HTMLPanel responseContent;
	@UiField Button closeButton;

	public Slide10(EventBus eventBus) {
		this.eventBus = eventBus;
		initWidget(binder.createAndBindUi(this));
		
		infoBox.setVisible(false);
		infoBox.setAnimationEnabled(true);
		infoBox.setGlassEnabled(true);
		
		feedPicker.clear();
	}

	@Override
	protected void onLoad() {
		slide.setFocus(true);
		feedPicker.clear();
		
		for(String s : feeds){
			feedPicker.addItem(s);
		}
		
		eventBus.addHandler(RequestJsonEvent.TYPE, new RequestJsonEvent.Handler() {
			
			@Override
			public void handleResult(JavaScriptObject obj) {
				if(obj != null){
					JSONObject jso = new JSONObject(obj);
					StringBuilder sb = new StringBuilder();
					
					JSONArray jsArray = jso.get("feed").isObject().get("entry").isArray();
					for(int i=0; i<jsArray.size(); i++){
						JSONObject entry = jsArray.get(i).isObject();
						JSONArray entryLinks = entry.get("link").isArray();
						for(int j=0; j<entryLinks.size(); j++){
							if(entryLinks.get(j).isObject().get("rel").toString().contains("alternate")){
								sb.append("<a href=" + entryLinks.get(j).isObject().get("href") + ">");
								sb.append(entry.get("title").isObject().get("$t").toString().replace("\"", "") + "</a><br/>");
								break;
							}
						}
					}
					
					responseContent.getElement().setInnerHTML(sb.toString());
				} else {
					responseContent.getElement().setInnerHTML("<b>Result was NULL. Please check your URL</b><br/>" + 
							"http://"+ feedPicker.getItemText(feedPicker.getSelectedIndex()) + URL);
				}
				infoBoxLabel.setHTML("<b>Recent Posts to the <i>"+ feedPicker.getItemText(feedPicker.getSelectedIndex()) + "</i> Blog</b>");
				infoBox.center();
			}
		});
	}

	@UiHandler("slide")
	public void keyPress(KeyDownEvent e){
		eventBus.fireEvent(new SlideTransitionEvent(e));
	}
	
	@UiHandler("jsonpRequestButton")
	public void onRequest(ClickEvent e){
		FetchData.getJson(1, "http://"+ feedPicker.getItemText(feedPicker.getSelectedIndex()) + URL, new Handler() {
			
			@Override
			public void handleResult(JavaScriptObject obj) {
				eventBus.fireEvent(new RequestJsonEvent(obj));
			}
		});
	}
	
	@UiHandler("closeButton")
	public void onCloseDialogBox(ClickEvent e){
		infoBox.hide();
	}

	public void setPresenter(Slide.Presenter listener){
		this.listener = listener;
	}

}